@extends('common.admin-master')
@section('html-header')
    <link rel="stylesheet" type="text/css" href="{{url('/css/admin/common.css')}}">
@endsection

@section('html-body')
    <div class="content">
        <div class="header">
            <h3 class="page-title">修改管理员密码</h3>
        </div>
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="well">
                    <div id="myTabContent" class="tab-content">
                        <div class="tab-pane active in" id="home" style="margin-left: 10%">
                            <ul class="fansinfo" style="list-style: none">
                                <li><em>新密码：</em><span><input type="password" id="pwd"/></span></li>
                                <li><em>确认密码：</em><span><input type="password" id="checkpwd"/></span></li>
                            </ul>
                            <div style="margin-left: 80px;margin-top: 20px">
                                    <button class="btn btn-primary save"><i class="icon-ok"></i>确定</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        jQuery(function() {
            var doc = document,
                    $doc = $(doc);

            function toggle() {
                $(".pop").toggle();
                $(".mirror").toggle();
            }

            $doc.on("click", ".eventBtn", function() {
                toggle();
            });

            $doc.on("click", ".mirror", function() {
                toggle();
            });

            $doc.on("click", ".pop button", function() {
                toggle();
            });

            jQuery.ajaxSetup({
                case:false,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            var c_newpwd = false;
            var c_checkpwd = false;

            $doc.on('click','.save',function(){
                var newpwd = $('#pwd').val();
                var checkpwd = $('#checkpwd').val();
                if(newpwd.length>=6 && newpwd.length<=20){
                    c_newpwd = true;
                    if(newpwd == checkpwd){
                        c_checkpwd = true;
                    }else{
                        noty({text:'密码输入不匹配',type:'error',timeout:500});
                    }
                }else{
                    noty({text:'密码需在6-20字符',type:'error',timeout:500});
                }
                if(c_newpwd && c_checkpwd) {
                    $.ajax({
                        type: "post",
                        url: "{{route('Api.Admin.changePwd')}}",
                        dataType: "json",
                        data: {
                            "userid": '{{$user->id}}',
                            "newpwd": newpwd
                        },
                        success: function (obj) {
                            if (obj.level === "success") {
                                noty({
                                    text: obj.message, type: obj.level, timeout: 800,
                                    callback: {
                                        afterClose: function () {
                                            location.reload();
                                        }
                                    }
                                });
                            }
                        },
                        error: function (obj) {
                            var datajson = obj.responseJSON;
                            noty({text: datajson.message, type: datajson.level, timeout: 800});
                        }
                    });
                }
            });
        });
    </script>
@endsection